package com.fast.web;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

@Controller
public class ChangeLangController {

	@Autowired
	private CookieLocaleResolver resolver;

	/**
	 * 语言切换
	 */
	@RequestMapping("/language")
	public ModelAndView language(HttpServletRequest request,
			HttpServletResponse response, String language) {

		if (language == null || language.equals("")) {
			return new ModelAndView("redirect:/");
		}

		language = language.toLowerCase();
		switch (language) {
		case "zh_cn":
			resolver.setLocale(request, response, Locale.CHINA);
			break;
		case "en":
			resolver.setLocale(request, response, Locale.ENGLISH);
			break;
		default:
			resolver.setLocale(request, response, Locale.CHINA);
		}

		return new ModelAndView("redirect:/");
	}
}
